package com.haruhinav.Utils;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Base64;

/**
 * 搜索工具类
 */
public class SearchUtil {

    /**
     * 根据搜索引擎返回url
     * @param engine
     * @param keyWord
     * @return
     * @throws MalformedURLException
     */
   public static URL selectUrl(String engine,String keyWord) throws MalformedURLException, UnsupportedEncodingException {

       URL url = null;
       switch (engine) {
           case Constants.BING_SEARCH_ENGINE:
               url = new URL(getBingURL(keyWord));
               break;
           case Constants.BAIDU_SEARCH_ENGINE:
               url = new URL(Constants.BAIDU_SEARCH_API + URLEncoder.encode(keyWord,"utf-8"));
               break;
           default:
               url = null;
               break;
       }
       return url;
   }

   private static String getBingURL(String keyWord) throws UnsupportedEncodingException {
       String urlEncoder = URLEncoder.encode(keyWord,"utf-8");
       String url = Constants.BING_SEARCH_API;
       url+="&bq="+urlEncoder;
       url+="&qry="+urlEncoder;
       return url;
   }


}
